package com.quartz.demo2;

import org.quartz.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.ResourceBundle;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.DateBuilder.*;
import static org.quartz.TriggerBuilder.*;

public class CronDemo {
	public static void main(String[] args) throws SchedulerException {
		ResourceBundle resource = ResourceBundle.getBundle("JobSettings");
		String startTime = resource.getString("START_TIME");
		String internal = resource.getString("INTERNAL");
		String apiName = resource.getString("API_NAME");
		String groupName = resource.getString("GROUP_NAME");
		String triggerNamePre = resource.getString("TRIGGER_NAME_PRE");

		SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
		Scheduler sched = schedFact.getScheduler();
		sched.start();

		// define the job and tie it to our HelloJob class
		JobDetail job = newJob(MyCronJob.class).withIdentity(apiName,groupName).build();

		LocalDateTime localDateTime = LocalDateTime.now().plusSeconds(5);

		Date date = localDateTime2Date(localDateTime);

		Trigger trigger = newTrigger()
				.withIdentity(triggerNamePre+apiName, groupName)
				.withSchedule(cronSchedule(internal))
				.forJob(apiName, groupName)
				.startAt(date)
				.build();
		 sched.scheduleJob(job,trigger);
		System.out.println("ddd:"+LocalDateTime.now());
	}

	public static Date localDateTime2Date(LocalDateTime localDateTime){
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
		Date date = Date.from(zdt.toInstant());
		return date;
	}
}
